#!/bin/bash

# 检查是否安装 Docker
echo -e "\n>>> check docker"
if [[ $(which docker | wc -l) -eq 0 ]]
then
    echo "docker ===> installing"

    sudo curl -sSL https://get.docker.com > docker_get.sh && \
    sh docker_get.sh --dry-run --mirror Aliyun && \
    sh docker_get.sh --mirror Aliyun && \
    rm -rf docker_get.sh && \
    systemctl start docker && systemctl enable docker

else
    echo "docker ===> ok"
fi
docker -v

# 检查是否安装 Docker-compose。新版 docker 自带 docker compose
# echo -e "\n>>> check docker-compose"
# if [[ $(which docker-compose | wc -l) -eq 0 ]]
# then
#     echo "docker-compose ===> installing"

#     # 从github下载比较慢
#     # sudo curl -L "https://github.com/docker/compose/releases/download/1.28.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
#     # 从国内源下载
#     sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/v2.12.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
#     sudo chmod +x /usr/local/bin/docker-compose && \
#     sudo chmod +x /usr/local/bin/docker-compose

# else
#     echo "docker-compose ===> ok"
# fi
# docker-compose -v

# 配置阿里镜像源
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://jdgbbr9w.mirror.aliyuncs.com"],
  "log-driver":"json-file",
  "log-opts": {"max-size":"100m", "max-file":"5"}
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker